package database;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

// memory database
public abstract class AbstractDatabase<T> implements Database<T> {
    private List<T> list;
    private ReentrantReadWriteLock lock;

    public AbstractDatabase() {
        list = new ArrayList<>();
        lock = new ReentrantReadWriteLock();
    }

    @Override
    public List<T> getList() {
        return list;
    }

    @Override
    public void startWrite() {
        lock.writeLock().lock();
    }

    @Override
    public void endWrite() {
        lock.writeLock().unlock();
    }

    @Override
    public void startRead() {
        lock.readLock().lock();
    }

    @Override
    public void endRead() {
        lock.readLock().unlock();
    }
}
